<?php
session_start ();
// 判断用户是否已经登录
if (! isset ( $_SESSION ["userinfo"] ) and empty ( $_SESSION ["userinfo"] ["userid"] )) {
	header ( "location:../index.php" );
	session_unset ();
	session_destroy ();
	exit ();
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="zh-CN">
<link href="./Resource/Style/base.yy.css" rel="stylesheet"
	type="text/css">
<link href="./Resource/Style/info.yy.css" rel="stylesheet"
	type="text/css">
</head>
<body>
<?php
require_once '../tools/SqlHelper.class.php';
if (isset ( $_POST ['user_submit'] ) and $_POST ['user_submit'] == "上传") {
	// echo '<pre>' . print_r ( $_FILES ["user_image"] ) . '</pre>';
	if ((($_FILES ["user_image"] ["type"] == "image/gif") || ($_FILES ["user_image"] ["type"] == "image/jpeg")) && ($_FILES ["user_image"] ["size"] < 4000000)) {
		if ($_FILES ["user_image"] ["error"] > 0) {
			// echo "出现错误，错误编码为: " . $_FILES ["user_image"] ["error"] . "<br />";
		} else {
			$user_id = $_SESSION ["userinfo"] ["userid"];
			// $user_id = "xufeng520";
			$path = "../PersonalPhoto/" . $user_id . "/";
			if (! file_exists ( $path )) {
				// 检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir ( $path, 0700 );
			}
			if (file_exists ( $path . $_FILES ["user_image"] ["name"] )) {
				// echo $_FILES ["user_image"] ["name"] . " 已经存在了. ";
				$user_image_name = "-1";
				// header ( "location:./uploadingPhoto.php?user_img=-1" );
			} else {
				$data = gettimeofday ();
				$name = "x";
				foreach ( $data as $key => $value ) {
					$name = $name . $value;
				}
				if ($_FILES ["user_image"] ["type"] == "image/gif") {
					$user_image_name = $name . ".gif";
				} else {
					$user_image_name = $name . ".jpg";
				}
				move_uploaded_file ( $_FILES ["user_image"] ["tmp_name"], $path . $user_image_name );
				// 文件信息
				// header ( "location:./uploadingPhoto.php?user_img=$user_image_name" );
				$buttonType = 1;
			}
		}
	} else {
		// echo "无效的文件";
		$user_image_name = "-2";
		// header ( "location:./uploadingPhoto.php?user_img=-2" );
	}
} else if (isset ( $_POST ['user_submit'] ) and $_POST ['user_submit'] == "保存") {
	$user_id = $_SESSION ["userinfo"] ["userid"];
	// $user_id = "xufeng520";
	$user_image_name = $_POST ['user_image_name'];
	$sqlHelper = new SqlHelper ();
	$sql = "UPDATE `personal_data_tb` SET `ps_Icon`='$user_image_name' WHERE `user_id`='$user_id'";
	$res = $sqlHelper->execute_dql ( $sql );
	if (! ! $res) {
		echo "<script>alert('保存头像成功')</script>";
	} else {
		echo "<script>alert('保存头像失败')</script>";
	}
	$buttonType = 0;
} else {
	$user_id = $_SESSION ["userinfo"] ["userid"];
	// $user_id = "xufeng520";
	$sqlHelper = new SqlHelper ();
	$sql = "select ps_Icon from `personal_data_tb` where `user_id`='$user_id'";
	$res = $sqlHelper->execute_dql ( $sql );
	$row = mysql_fetch_array ( $res );
	$user_image_name = $row ['ps_Icon'];
	$buttonType = 0;
}
?>
	<nav>
		<div class="info_nav">
			<ul>
				<li><a href="./baseData.php">基本资料</a></li>
				<li><a href="./Details.php">详细资料</a></li>
				<li><a href="./soliloquy.php">内心独白</a></li>
				<li><a class="a_out" href="#">上传头像</a></li>
				<li><a href="./myPhoto.php">我的相册</a></li>
				<li><a href="./PersonalCondition.php">征友条件</a></li>
				<li><a href="./changePassword.php">修改密码</a></li>
				<li class="last"><a href="#"></a></li>
			</ul>
		</div>
	</nav>
	<div class="info_con list_info upload_logo_step1">
		<div class="photo">
			<form action="./uploadingPhoto.php" method="post"
				enctype="multipart/form-data">
				<input type="hidden" value="<?php echo $user_image_name;?>"
					name="user_image_name" /> <img
					src=<?php
					if ($user_image_name == "0" || $user_image_name == "-1" || $user_image_name == "-2") {
						$user_img_path = "./Resource/Images/photo_boy.jpg";
						echo $user_img_path;
					} else {
						$user_id = $_SESSION ["userinfo"] ["userid"];
						$path = "../PersonalPhoto/" . $user_id . "/";
						$user_image_name = $path . $user_image_name;
						echo "'" . $user_image_name . "' style='width: 200px;height: 200px;'";
					}
					?> />
					<?php
					if ($user_image_name == "-1") {
						echo "图片已存在";
					} else if ($user_image_name == "-2") {
						echo "无效文件";
					}
					?>
			<div class="photo_btn">
					<input class="computer" name="user_image" type="file" value="选择文件" />
					<input class="video" name="user_submit" type="submit"
						value=<?php
						if ($buttonType == 0) {
							echo "上传";
						} else {
							echo "保存";
						}
						?> />
				</div>
			</form>
			<dl>
				<dt>为什么要上传照片</dt>
				<dd>1.上传照片是征友诚意的体现。</dd>
				<dd>2.没有照片很难获得异性联系。</dd>
				<dd>3.获得优先推荐，更快找对象。</dd>
				<dd>4.在搜索结果中排名靠前。</dd>
				<dd>5.全方位展示自己，更快找对象，有照片的用户征友成功率是无照片用户的20倍！</dd>
			</dl>
		</div>
	</div>
</body>
</html>